package com.fangfaze.java.hcwork.liveLifeCycle.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamsNotifyUrlConfigRequest;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamsNotifyUrlConfigResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.fangfaze.java.hcwork.liveLifeCycle.entity.LiveUrls;
import com.fangfaze.java.hcwork.liveLifeCycle.service.AliyunLiveService;
import com.fangfaze.java.hcwork.liveLifeCycle.utils.UrlSignUtil;
import org.springframework.stereotype.Service;

@Service
public class AliyunLiveServiceImpl implements AliyunLiveService {

    IAcsClient client = new DefaultAcsClient(
            DefaultProfile.getProfile(
                    "cn-beijing",
                    "LTAID3huPeW6HsjK",
                    "urpbNqb9K4cCWgAAw00bPSZxm6VbeX"
            )
    );

    public static final String SCHEME = "rtmp://";
    public static final String PULL_HOST = "live.8d8u.com";
    public static final String PULL_KEY = "nYkdbUN8DTkF8zvy";

    public static final String PUSH_HOST = "push.8d8u.com";
    public static final String PUSH_KEY = "sbrTso3LBlhr0I7m";


    @Override
    public LiveUrls getStramPair(String applicationId, String streamId) {
        LiveUrls liveUrls = new LiveUrls();
        liveUrls.setIntopieceId(applicationId);
        liveUrls.setId(streamId);
        String timeStamp = String.valueOf(System.currentTimeMillis() + 30 * 60 * 1000);
        liveUrls.setPullUrl(UrlSignUtil.generate(SCHEME, PULL_HOST, applicationId, streamId, PULL_KEY, timeStamp));
        liveUrls.setPushUrl(UrlSignUtil.generate(SCHEME, PUSH_HOST, applicationId, streamId, PUSH_KEY, timeStamp));
        return liveUrls;
    }

    @Override
    public DescribeLiveStreamsNotifyUrlConfigResponse.LiveStreamsNotifyConfig getConfig() {

        DescribeLiveStreamsNotifyUrlConfigRequest request = new DescribeLiveStreamsNotifyUrlConfigRequest();
        request.setDomainName("live.8d8u.com");
        try {
            DescribeLiveStreamsNotifyUrlConfigResponse response = client.getAcsResponse(request);
            return response.getLiveStreamsNotifyConfig();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
